package com.groot.medical.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.google.gson.Gson;
import com.groot.common.controller.BaseServletController;

import com.groot.medical.entity.Menu;
import com.groot.medical.entity.MenuTreeDto;
import com.groot.medical.entity.SysUser;
import com.groot.medical.service.MenuService;
import com.groot.medical.service.MenuServiceImpl;
import com.groot.medical.service.UserService;
import com.groot.medical.service.UserServiceImpl;










@WebServlet("/login.do")
public class LoginController extends BaseServletController{
	
	private UserService userService = new UserServiceImpl();
	
	private MenuService menuService = new MenuServiceImpl();
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(LoginController.class);
	public String execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LOGGER.info("this is @WebServlet(\"/index.do\").execute()");
		return null;
	}
	/**
	 * 打开登录界面
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		return "login.jsp";
	}
	/**
	 * 打开主页
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String toMain(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取用户session
		SysUser sysUser = (SysUser) request.getSession().getAttribute("sysUser");
		//通过用户id查询对应权限
		List<Menu> menuList = menuService.getMenuByUserId(sysUser.getUserId());
		System.out.println("_+_+"+menuList);
		//将menuList存入session
		request.getSession().setAttribute("menuList", menuList);
		//初始化配置树状结构的权限菜单
		List<MenuTreeDto> menuTree = initMenuTreeDto(menuList);
		System.out.println("+_+_++_+:"+menuTree);
		
		LOGGER.info("menuList:" + menuList);
		
		LOGGER.info("menuTree:" + menuTree);
		Gson gson = new Gson();
		request.getSession().setAttribute("menuTree", gson.toJson(menuTree));
		
		
		
		System.out.println("menuList"+menuList);
		
		return "WEB-INF/page/index.jsp";
	}
	/**
	 * 初始化权限菜单数据
	 * @param menuList
	 * @return
	 */
	private List<MenuTreeDto> initMenuTreeDto(List<Menu> menuList) {
		List<Menu> parents = new ArrayList<Menu>();//一级菜单
		for(int i=0; i<menuList.size(); i++){
			if(menuList.get(i).getMenuPid().equals("0")){
				parents.add(menuList.get(i));
				menuList.remove(i);
			}
		}
		
		List<MenuTreeDto> menuTree = new ArrayList<MenuTreeDto>();
		
		for(Menu parent : parents){
			MenuTreeDto treeDto = new MenuTreeDto();
			
			List<Menu> item = new ArrayList<Menu>();
			for(int i=0; i<menuList.size(); i++){
				if(parent.getMenuId().equals(menuList.get(i).getMenuPid())){
					treeDto.setMenu(parent);
					item.add(menuList.get(i));
				}
			}
			treeDto.setItems(item);
			menuTree.add(treeDto);
		}
		return menuTree;
	}
	
	/**
	 * 用户登录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	
	public String userLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String userId = request.getParameter("userId");
		String password = request.getParameter("password");
		SysUser sysUser = userService.findById(userId, password);
		LOGGER.info("用户信息："+sysUser);
		if(sysUser!=null){
			request.getSession().setAttribute("sysUser", sysUser);
			response.sendRedirect("login.do?method=toMain");
			return null;
			/*redirect:/medical/main.do?method=toMain*/
		}else
			return "login.jsp";
		
		}
	/**
	 * 退出登录
	 * @param request
	 * @param response
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String outLogin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		  request.getSession().invalidate();  
		    return "/login.jsp"; 
	}

}
